/**
 * Filename: LocalManageAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/17 15:22
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.LocalManageActivity;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageUtils;
import com.github.postsanf.yinian.utils.LocalViewHolder;

import java.util.List;

public class LocalManageAdapter extends CommonAdapter<String> {
    private Activity activity;
    private Resources mResource;

    public LocalManageAdapter(Context context, List<String> mDatas, int itemLayoutId, Activity activity) {
        super(context, mDatas, itemLayoutId, 1);
        this.activity = activity;
    }

    public void setResource(Resources mResource) {
        this.mResource = mResource;
    }

    @Override
    public void convert(final LocalViewHolder helper, final String item, final Boolean isFirst) {
        final SimpleDraweeView mImageView = helper.getView(R.id.iv_album_grid_item_image);
        final ImageView mSelect = helper.getView(R.id.id_item_select);
        if (isFirst) {
            helper.setResource(mResource);
            helper.setImageResource(R.id.iv_album_grid_item_image, R.drawable.tocamera);
            mSelect.setVisibility(View.GONE);
        } else {
            //设置no_pic
//			helper.setImageResource(R.id.iv_album_grid_item_image, R.drawable.pictures_null);
            //设置no_selected
            helper.setImageResource(R.id.id_item_select, R.drawable.picture_unselected);
            helper.setImageByUrl(R.id.iv_album_grid_item_image, item);
            mSelect.setVisibility(View.VISIBLE);
        }
        // 设置图片宽高
        int screenWidth = DisplayUtils.getScreenWidthPixels(activity);
        int horizontalSpacing = DisplayUtils.dp2px(activity, 2);
        int itemWidth = (screenWidth - 2 * horizontalSpacing) / 3;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
        mImageView.setLayoutParams(params);
        mImageView.setColorFilter(null);
        //设置ImageView的点击事件
        mImageView.setOnClickListener(new View.OnClickListener() {
            //选择，则将图片变暗，反之则反之
            @Override
            public void onClick(View v) {
                if (isFirst) {
                    ImageUtils.pickImageFromCamera(activity);
                    return;
                }
                // 已经选择过该图片
                if (SelectedImg.mSelectedImage.contains(item)) {
                    SelectedImg.mSelectedImage.remove(item);
                    if (mResource != null) {
                        mSelect.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("picture_unselected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
                    } else {
                        mSelect.setImageResource(R.drawable.picture_unselected);
                    }
                    mImageView.setColorFilter(null);
                } else
                // 未选择该图片
                {
                    SelectedImg.mSelectedImage.add(item);
                    if (mResource != null) {
                        mSelect.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("pictures_selected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
                    } else {
                        mSelect.setImageResource(R.drawable.pictures_selected);
                    }
                    mImageView.setColorFilter(Color.parseColor("#33000000"));
                }
                ((LocalManageActivity) activity).setSureCount(SelectedImg.mSelectedImage.size());
            }
        });
        // 已经选择过的图片，显示出选择过的效果
        if (SelectedImg.mSelectedImage.contains(item)) {
            if (mResource != null) {
                mSelect.setImageDrawable(mResource.getDrawable(mResource.getIdentifier("pictures_selected", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
            } else {
                mSelect.setImageResource(R.drawable.pictures_selected);
            }
            mImageView.setColorFilter(Color.parseColor("#33000000"));
        }
    }
}